package com.wlyuan.gateway.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yuanzheng
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClaimUtils {
    private static final Map<String, String> CLAIMS_MAPPING = new HashMap<>();

    static {
        CLAIMS_MAPPING.put("tenantId", "userCompanyid");
        CLAIMS_MAPPING.put("userId", "sub");
        CLAIMS_MAPPING.put("mobile", "userPhone");
        CLAIMS_MAPPING.put("appId", "appId");
        CLAIMS_MAPPING.put("username", "username");
        CLAIMS_MAPPING.put("consignees", "consignees");
        CLAIMS_MAPPING.put("scene", "scene");
    }

    public static Map<String, Object> convert(Map<String, Object> oldClaims) {
        Map<String, Object> claims = new HashMap<>(16);
        if (CollectionUtils.isEmpty(oldClaims)) {
            return claims;
        }

        CLAIMS_MAPPING.forEach((newName, oldName) -> {
            if (oldClaims.containsKey(newName)) {
                claims.put(newName, oldClaims.get(newName));
            }
            else if (oldClaims.containsKey(oldName)) {
                claims.put(newName, oldClaims.get(oldName));
            }
        });
        return claims;
    }
}
